package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.server.mapper.RoleMapper;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.Role;
import com.xxxx.server.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-08-17
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

	@Autowired
	private RoleMapper roleMapper;

	/**
	 * 根据用户id查询角色g
	 * @param adminId
	 * @return
	 */
	@Override
	public List<Role> getAllRolesByAdminId(Integer adminId) {
		return roleMapper.getAllRolesByAdminId(adminId);
	}

	@Override
	public List<Role> getAllRoles() {
		return roleMapper.getAllRoles();
	}

	/**
	 * 添加角色
	 * @param role
	 * @return
	 */
	@Override
	public RespBean addRole(Role role) {
		if (null==role.getNameZh() ||role.getNameZh()=="") {
			RespBean.error("角色名称不能为空");
		}


		if (null==role.getName() ||role.getName()=="") {
		return 	RespBean.error("名称不能为空");
		}
		if (null!=roleMapper.queryRoleByNameZh(role.getNameZh())){
			System.out.println(11111111);
		return 	RespBean.error("角色名称已存在，请重新输入");
		}
		if (null!=roleMapper.queryRoleByName(role.getName())){
			return RespBean.error("名称不能为空");
		}

		if (role.getName().startsWith("ROLE_")){
			role.setName("ROLE_"+role.getName());
		}
		if (roleMapper.addRole(role)){
			return RespBean.success("添加成功");

		}
		else {
			return RespBean.error("添加失败");

		}
	}

	/**
	 * 删除角色
	 * @param id
	 * @return
	 */
	@Override
	public RespBean deleteRole(Integer id) {


		if(roleMapper.deleteRole(id)){
			return RespBean.success("删除角色成功");
		}
		else {

			return RespBean.error("删除角色失败");
		}
	}

}
